const express = require("express");
const app = express();
//var rePost = require('body-parser');
var rest = require('./rest');//相对路径
var filter = require('./filter');

//模糊查询地标名称，返回经纬度接口
app.get("/query/match/layer/name", function (req, res) {
	const query = {};
	for (let p in req.query) {
		if (/layerName/i.test(p)) {
			query.layerName = req.query[p];
		} else if (/queryName/i.test(p)) {
			query.queryName = req.query[p];
		}
	}
	rest.queryQuery(query.layerName).then(function (data) {
		//获取所有name数据，并取唯一值
		return filter.nameFilter(data.features);
	}).then(function (names) {
		var strs = query.queryName.toUpperCase().split(",");//字符分割
		return strs.reduce(function (previous, current) {
			return previous.then(function (data) {
				return names = filter.fuzMatch(current, names);
			});
		}, Promise.resolve([])).then(function (results) {
			return rest.queryLocators(query.layerName, results);
		}).then(function (midRes) {
			//注意这里的filter.resultsFilter(midRes)的返回结果不是promise,不能用then()方法
			var finalRes = filter.resultsFilter(midRes);
			//reduce可以循环嵌套
			return strs.reduce(function (previous, current) {
				return previous.then(function (data) {
					return filter.fuzMatchObj(current, finalRes);
				});
			}, Promise.resolve([]));
		});
	}).then(function (resData) {
		//解决跨域问题
		res.append("Access-Control-Allow-Origin", "*");
		res.json({
			respCode: 0,
			respMsg: "成功",
			respCount: resData.length,
			data: resData
		});
	}, function () {
		res.json({
			respCode: -1,
			respMsg: "请求失败"
		});
	});
});


//查询建筑物名称返回外轮廓经纬度的接口
app.get("/query/layer/name/border", function (req, res) {
	//去掉layerName,queryName大小写敏感的问题
	const query = {};
	for (let p in req.query) {
		if (/layerName/i.test(p)) {
			query.layerName = req.query[p];
		} else if (/queryName/i.test(p)) {
			query.queryName = req.query[p];
		}
	}

	rest.queryQuery(query.layerName).then(function (data) {
		//获取所有name数据，并取唯一值
		return filter.nameFilter(data.features);
	}).then(function (names) {
		var strs = query.queryName.toUpperCase().split(",");//字符分割
		return strs.reduce(function (previous, current) {
			return previous.then(function (data) {
				return names = filter.fuzMatch(current, names);
			});
		}, Promise.resolve([])).then(function (results) {
			return rest.queryFind(query.layerName, results);
		});
	}).then(function (resData) {
		//跨域问题
		res.append("Access-Control-Allow-Origin", "*");
		res.json({
			respCode: 0,
			respMsg: "成功",
			respCount: resData.length,
			data: resData
		});
	}, function () {
		res.json({
			respCode: -1,
			respMsg: "请求失败"
		});
	});
});


//查询地标名称返回周围建筑经纬度的接口
//底层的，不涉及Name了，直接到缓冲区那一步，输入geometry,和缓冲距离即可，最后再做identity
app.get("/query/name/round", function (req, res) {
	//参数形式：/query/name/round？queryName=&distance=
	const query = {};
	for (let p in req.query) {
		if (/queryName/i.test(p)) {
			query.queryName= req.query[p];
		} else if (/distance/i.test(p)) {
			query.distance= req.query[p];
		}
	}
	rest.queryBuffer(query).then(function (data) {
		var resData=filter.identyLocator(data);
		//跨域问题
		res.append("Access-Control-Allow-Origin", "*");
		res.json({
			respCode: 0,
			respMsg: "成功",
			respCount: resData.length,
			data: resData
		});
	}, function () {
		res.json({
			respCode: -1,
			respMsg: "请求失败"
		});
	});

});
/*app.post("/query/round/geometry", bodyParser.json(),function (req, res) {
	//参数形式：/query/round/geometry？geo=&distance=
	const query = {};
	for (let p in req.query) {
		if (/geo/i.test(p)) {
			query.geo = req.query[p];
		} else if (/dis/i.test(p)) {
			query.distance= req.query[p];
		}
	}


});*/

//距离查询接口,底层的，由前台传入
app.get("/query/measure/geometry", function (req, res) {
	//参数形式：/query/measure/geometry?geos=
	//去掉URL参数大小写敏感的限制
	const query = {};
	for (let p in req.query) {
		if (/geos/i.test(p)) {
			query.geos = req.query[p];
		} 
	}
	//重组ArcGIS输入需要的Geometry格式
	var geos = filter.createGeometry(query.geos);

	return rest.queryDistance(geos).then(function (resData) {
		//跨域问题
		res.append("Access-Control-Allow-Origin", "*");
		res.json({
			respCode: 0,
			respMsg: "成功",
			//respCount: resData.length,
			data: resData
		});
	}, function () {
		res.json({
			respCode: -1,
			respMsg: "请求失败"
		});
	});

});


app.listen(1116, function () {
	console.log("Server bounded.")
});





